/**
* Created with IntelliJ IDEA.
*/
var url_gridData =Constants.ctx+ "${bean.lowerClassName}/getGridData.do";
var url_grid = Constants.ctx+"${bean.lowerClassName}/grid.do";
var url_add = Constants.ctx+"${bean.lowerClassName}/add.do";
var url_edit = Constants.ctx+"${bean.lowerClassName}/edit.do";
var url_view = Constants.ctx+"${bean.lowerClassName}/view.do";
var url_delete = Constants.ctx+"${bean.lowerClassName}/delete.do";
var url_deleteMore = Constants.ctx+"${bean.lowerClassName}/deleteMore.do";
var url_save = Constants.ctx+"${bean.lowerClassName}/save.do";

function crud(value, row, index) {
var viewButton = "<a href=\"#\" onclick=\"dealView('" + index + "')\">查看</a>   ";
var editButton = "<a href=\"#\" onclick=\"dealEdit('" + index + "')\">编辑</a>   ";
var deleteButton = "<a href=\"#\" onclick=\"dealDelete('" + index + "')\">删除</a>   ";
var operate = '';
operate += viewButton;
operate += editButton;
operate += deleteButton;
return operate;
}
//view
function dealView(index) {
$('#' + Constants.gridId).datagrid('selectRow', index);
function callBack(row) {
var urlParams = '?id=' + row.id;
loadView({urlParams: url_view + urlParams, data: row});
}
Utils.doView(callBack, {elementId: Constants.gridId});
}

//edit
function dealEdit(index) {
$('#' + Constants.gridId).datagrid('selectRow', index);
function callBack(row) {
var urlParams = '?id=' + row.id;
loadInput({urlParams: url_edit + urlParams, data: row});
}

Utils.doEdit(callBack, {elementId: Constants.gridId});
}

//delete
function dealDelete(index) {
$('#' + Constants.gridId).datagrid('selectRow', index);
var row = $('#' + Constants.gridId).datagrid('getSelected');

function callBack(flag) {
}

Utils.doDelete(callBack, {elementId: Constants.gridId, url: url_delete, id: row.id});
}


$(document).ready(function () {
loadGrid();
})

function loadGrid() {
$("#" + Constants.containerId).load(url_grid, function () {
var params = {};
params.title="${bean.comment}列表"
params.elementId = Constants.gridId;
params.url = url_gridData;
params.toolbar = [
{
iconCls: 'icon-remove',
text: '批量删除',
handler: function () {
function callBack(flag) {
}
Utils.doDeleteMore(callBack, {elementId: Constants.gridId, url: url_deleteMore});
}
},
'-',
{
iconCls: 'icon-add',
text: '添加',
handler: function () {
loadAdd({ data: {}});
}
}
];
params.columns = [
[
{field: 'ck', checkbox: true},
{field: "id", title: "ID",width: Utils.fillsize(1 / ${bean.fieldSize+7})},
<#list bean['fields'] as field>
${field.columnUI},
</#list>
{field: 'description', title: '备注', width: Utils.fillsize(1 / ${bean.fieldSize+7})},
//{field: 'createTime', title: '创建时间', width: Utils.fillsize(1 / ${bean.fieldSize+7})},
//{field: 'createUser', title: '创建人', width: Utils.fillsize(1 / ${bean.fieldSize+7})},
//{field: 'updateTime', title: '更新时间', width: Utils.fillsize(1 / ${bean.fieldSize+7})},
//{field: 'updateUser', title: '更新人', width: Utils.fillsize(1 / ${bean.fieldSize+7})} ,
{field: 'operator', title: '操作', hidden: false, align: 'right', width: Utils.fillsize(1 / ${bean.fieldSize+7}),
formatter: crud
}
]
];
Utils.initGrid(params);
})
}

function loadInput(moduleParams) {
$("#" + Constants.containerId).load(moduleParams.urlParams, function () {
$('#' + Constants.paneId).panel({
title: '编辑${bean.comment}',
fit: true
});
initComplexObject(moduleParams);
$('#' + Constants.formId).form('load', moduleParams.data);
validation();
registeEvent(moduleParams);
})
}

function loadAdd(moduleParams) {
$("#" + Constants.containerId).load(url_add, function () {
//处理信息
$('#' + Constants.paneId).panel({
title: '添加${bean.comment}',
fit: true
});
$('#' + Constants.formId).form();
validation();
registeEvent(moduleParams);
})
}

function loadView(moduleParams) {
$("#" + Constants.containerId).load(moduleParams.urlParams, function () {
//处理信息
$('#' + Constants.paneId).panel({
title: '查看${bean.comment}',
fit: true
});
initComplexObject(moduleParams);
$('#' + Constants.formId).form('load', moduleParams.data);
$('#' + Constants.formId).find('input').each(function () {
$(this).attr('disabled', 'disabled');
$(this).attr('readonly', true);
})
registeEvent(moduleParams);
})
}

function validation() {
var conditions = {
//        id: {},
<#list bean['fields'] as field>
    <#if field.columnConditionUI != "">
    ${field.columnConditionUI},
    </#if>
</#list>
description: {},
createTime: {element: 'datetimebox', formatter: Utils.formaterDateTime},
createuser: {},
updateTime: {element: 'datetimebox', formatter: Utils.formaterDateTime},
updateuser: {}
};
Utils.initFormConditions(Constants.formId, conditions);
}

function registeEvent(moduleParams) {
$('#' + Constants.save_button).click(function () {
var obj = {};
obj.elementId = Constants.formId;
obj.url = url_save;
function dealAjaxBack(data) {
$.messager.alert('Info', data, 'info');
loadGrid();
Utils.refreshGrid({formId: Constants.formId});
}
obj.dealAjaxBack = dealAjaxBack;
Utils.submitForm(obj);
});
$('#' + Constants.reset_button).click(function () {
Utils.resetForm(Constants.formId);
});
$('#' + Constants.back_button).click(function () {
loadGrid();
})
}

function initComplexObject(moduleParams) {
<#list bean['fields'] as field>
    <#if field.relationObject!="">
    moduleParams.data['${field.relationObject}.id'] = moduleParams.data.${field.relationObject}.id;
    moduleParams.data['${field.relationObject}.name'] = moduleParams.data.${field.relationObject}.name;
    </#if>
</#list>
}